package org.example.chap03;

import java.util.Arrays;

/**
 * https://leetcode-cn.com/problems/valid-anagram/
 */
public class isAnagram {


    public static void main(String[] args) {
//        String s = "anagram", t = "nagarmm";

        String s = "aaa", t = "abc";
//        System.out.println(solution1(s,t));
        System.out.println(solution2(s,t));
    }

    public static boolean solution1(String s,String t){
        if(s.length()!=t.length()){
            return false;
        }

        char[] str1 = s.toCharArray();
        char[] str2 = t.toCharArray();
        Arrays.sort(str1);
        Arrays.sort(str2);
        return Arrays.equals(str1,str2);
    }

    /**
     * 使用哈希表去实现
     * @param s
     * @param t
     * @return
     */
    public static boolean solution2(String s,String t){
        if(s.length() != t.length()){
            return false;
        }
        int[] table = new int[26];
        for (int i = 0; i < s.length(); i++) {
            System.out.println(s.charAt(i)-'a');
            table[s.charAt(i)-'a']++;
        }
        System.out.println("=========================");
        for (int i = 0; i < t.length(); i++) {
            System.out.println(t.charAt(i)-'a');
            table[t.charAt(i)-'a']--;
            if(table[t.charAt(i)-'a']<0){
                return false;
            }
        }
        return true;
    }
}
